const app = getApp()
const commissionApi = require('../../../utils/commissionApi.js');

Page({
  data: {
    withdrawList: [],
    page: 1,
    per_page: 10,
    total: 0,
    pages: 1,
    statusFilter: '', // 过滤状态，''表示全部
    loading: false,
    noMore: false,
  },

  onLoad() {
    this.loadWithdrawList();
  },

  // 切换状态筛选
  onStatusChange(e) {
    const status = e.detail.value; // 假设你用picker或segment选择状态
    this.setData({
      statusFilter: status,
      page: 1,
      withdrawList: [],
      noMore: false,
    });
    this.loadWithdrawList();
  },

  loadWithdrawList() {
    if (this.data.loading || this.data.noMore) return;

    this.setData({ loading: true });

    commissionApi.getWithdrawList({
      page: this.data.page,
      per_page: this.data.per_page,
      status: this.data.statusFilter || undefined,
    })
      .then(res => {
        console.log(res);
        if (res.data && res.data.items) {
          const newList = this.data.withdrawList.concat(res.data.items);
          const noMore = this.data.page >= res.data.pages;

          this.setData({
            withdrawList: newList,
            total: res.data.total,
            pages: res.data.pages,
            noMore,
            page: this.data.page + 1,
          });
        } else {
          wx.showToast({ title: '数据加载异常', icon: 'error' });
        }
      })
      .catch(() => {
        wx.showToast({ title: '请求失败，请稍后重试', icon: 'error' });
      })
      .finally(() => {
        this.setData({ loading: false });
      });
  },


  // 页面上拉触底，加载更多
  onReachBottom() {
    this.loadWithdrawList();
  },
});
